package com.spzx.auth.controller;

import com.spzx.auth.form.LoginBody;
import com.spzx.auth.form.RegisterBody;
import com.spzx.auth.service.H5LoginService;
import com.spzx.common.core.domain.R;
import com.spzx.common.security.service.TokenService;
import com.spzx.system.api.model.LoginUser;
import com.spzx.user.api.domain.UserInfo;
import io.swagger.v3.oas.annotations.Operation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RestController;

/**
 * ClassName: H5TokenController
 * Package: com.spzx.auth.controller
 * Description:
 */
@RestController
public class H5TokenController {
    @Autowired
    private H5LoginService h5LoginService;
    @Autowired
    private TokenService tokenService;


    @Operation(summary = "注册")
    @PostMapping("/h5/register")
    public R<?> register(@RequestBody RegisterBody registerBody)
    {
        // 用户注册
        return h5LoginService.register(registerBody);
    }

    @Operation(summary = "登录")
    @PostMapping("/h5/login")
    public R<?> login(@RequestBody LoginBody from){
        //用户登录
        LoginUser userInfo = h5LoginService.login(from.getUsername(), from.getPassword());
        //获取登录token
        return R.ok(tokenService.createToken(userInfo));
    }

}
